package top.wardyan.wdev.dbdevassist.action.name;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;

import top.wardyan.conf.Const;
import top.wardyan.control.WComposite;
import top.wardyan.control.WLabel;
import top.wardyan.control.WText;
import top.wardyan.util.Msg;
import top.wardyan.util.ShellUtil;
import top.wardyan.wdev.dbdevassist.action.base.BaseDetailEditShell;
import top.wardyan.wdev.dbdevassist.action.base.BaseMasterEditShell;
import top.wardyan.wdev.dbdevassist.action.dic.DicItemEditShell;
import top.wardyan.wdev.dbdevassist.config.DDA;
import top.wardyan.wdev.dbdevassist.model.YTDic;
import top.wardyan.wdev.dbdevassist.model.YTDicItem;
import top.wardyan.wdev.dbdevassist.model.YTName;
import top.wardyan.wdev.dbdevassist.service.IDetailService;
import top.wardyan.wdev.dbdevassist.service.IMasterService;

@SuppressWarnings("unused")
public class NameEditShell extends BaseMasterEditShell<NameEditShell, YTName>{

	
	protected WText txtXField;
	protected WText txtXName;
	protected WText txtXNote;
	
	
	public NameEditShell() {
		this(1, DDA.getInstance().nameService);
	}
	
	public NameEditShell(int sID, IMasterService<YTName> masterService) {
		super(sID, masterService);
		this.createContent();
		this.txtXField.setFocus();
		
		
	}
	
	private void createContent() {
		
//		WComposite comSID = new WComposite(mainCom, SWT.NONE);
//		comSID.setLayout(new FormLayout());
//		FormData fd_comSID = new FormData();
//		fd_comSID.left = new FormAttachment(mainCom, 4);
//		fd_comSID.top = new FormAttachment(mainCom, 0);
//		//fd_comSID.right = new FormAttachment(comSID, 300);
//		fd_comSID.right = new FormAttachment(25, 0);
//		fd_comSID.bottom = new FormAttachment(comSID, 35);
//		comSID.setLayoutData(fd_comSID);
//		
//		WLabel lblSID = new WLabel(comSID, SWT.BORDER, Const.FONT16, "sID", SWT.RIGHT);
//		FormData fd_lblSID = new FormData();
//		fd_lblSID.left = new FormAttachment(comSID, 0);
//		fd_lblSID.top = new FormAttachment(comSID, 2);
//		fd_lblSID.right = new FormAttachment(30);
//		fd_lblSID.bottom = new FormAttachment(lblSID, 30);
//		lblSID.setLayoutData(fd_lblSID);
//		txtSID = new WText(comSID, SWT.BORDER, "", "");
//		txtSID.setTextLimit(9);
//		txtSID.addKeyListener(new WNumberOnlyKeyAdapter());
//		FormData fd_txtSID = new FormData();
//		fd_txtSID.left = new FormAttachment(lblSID, 4);
//		fd_txtSID.top = new FormAttachment(comSID, 2);
//		fd_txtSID.right = new FormAttachment(100, -8);
//		fd_txtSID.bottom = new FormAttachment(txtSID, 30);
//		txtSID.setLayoutData(fd_txtSID);
//		txtSID.setEditable(false);
		
		WComposite comMain = new WComposite(this, SWT.NONE);
		comMain.setLayout(new FormLayout());
		FormData fd_comMain = new FormData();
		fd_comMain.left = new FormAttachment(this, 4);
		fd_comMain.top = new FormAttachment(topCom, 4);
		fd_comMain.right = new FormAttachment(100, -3);
		//fd_comMain.bottom = new FormAttachment(comXName, 35);
		comMain.setLayoutData(fd_comMain);
		
		
		WComposite comXField = new WComposite(comMain, SWT.NONE);
		comXField.setLayout(new FormLayout());
		FormData fd_comXField = new FormData();
		fd_comXField.left = new FormAttachment(comMain, 4);
		fd_comXField.top = new FormAttachment(comMain, 4);
		fd_comXField.right = new FormAttachment(33, 0);
		fd_comXField.bottom = new FormAttachment(comXField, 35);
		comXField.setLayoutData(fd_comXField);
		
		WLabel lblXField = new WLabel(comXField, SWT.BORDER, Const.FONT16, "字段", SWT.RIGHT);
		FormData fd_lblXField = new FormData();
		fd_lblXField.left = new FormAttachment(comXField, 0);
		fd_lblXField.top = new FormAttachment(comXField, 2);
		fd_lblXField.right = new FormAttachment(30);
		fd_lblXField.bottom = new FormAttachment(lblXField, 30);
		lblXField.setLayoutData(fd_lblXField);
		txtXField = new WText(comXField, SWT.BORDER, "", "字段");
		txtXField.setTextLimit(128);
		FormData fd_txtXField = new FormData();
		fd_txtXField.left = new FormAttachment(lblXField, 4);
		fd_txtXField.top = new FormAttachment(comXField, 2);
		fd_txtXField.right = new FormAttachment(100, -8);
		fd_txtXField.bottom = new FormAttachment(txtXField, 30);
		txtXField.setLayoutData(fd_txtXField);
		
		
		WComposite comXName = new WComposite(comMain, SWT.NONE);
		comXName.setLayout(new FormLayout());
		FormData fd_comXName = new FormData();
		fd_comXName.left = new FormAttachment(comXField, 4);
		fd_comXName.top = new FormAttachment(comMain, 4);
		fd_comXName.right = new FormAttachment(70, 0);
		fd_comXName.bottom = new FormAttachment(comXName, 35);
		comXName.setLayoutData(fd_comXName);
		
		WLabel lblXName = new WLabel(comXName, SWT.BORDER, Const.FONT16, "名称", SWT.RIGHT);
		FormData fd_lblXName = new FormData();
		fd_lblXName.left = new FormAttachment(comXName, 0);
		fd_lblXName.top = new FormAttachment(comXName, 2);
		fd_lblXName.right = new FormAttachment(30);
		fd_lblXName.bottom = new FormAttachment(lblXName, 30);
		lblXName.setLayoutData(fd_lblXName);
		txtXName = new WText(comXName, SWT.BORDER, "", "名称");
		txtXName.setTextLimit(128);
		FormData fd_txtXName = new FormData();
		fd_txtXName.left = new FormAttachment(lblXName, 4);
		fd_txtXName.top = new FormAttachment(comXName, 2);
		fd_txtXName.right = new FormAttachment(100, -8);
		fd_txtXName.bottom = new FormAttachment(txtXName, 30);
		txtXName.setLayoutData(fd_txtXName);
		
		
		WComposite comXNote = new WComposite(comMain, SWT.NONE);
		comXNote.setLayout(new FormLayout());
		FormData fd_comXNote = new FormData();
		fd_comXNote.left = new FormAttachment(comXName, 4);
		fd_comXNote.top = new FormAttachment(comMain, 4);
		fd_comXNote.right = new FormAttachment(100, -2);
		fd_comXNote.bottom = new FormAttachment(comXNote, 35);
		comXNote.setLayoutData(fd_comXNote);
		WLabel lblXNote = new WLabel(comXNote, SWT.BORDER, Const.FONT16, "备注", SWT.RIGHT);
		FormData fd_lblXNote = new FormData();
		fd_lblXNote.left = new FormAttachment(comXNote, 0);
		fd_lblXNote.top = new FormAttachment(comXNote, 2);
		fd_lblXNote.right = new FormAttachment(30);
		fd_lblXNote.bottom = new FormAttachment(lblXNote, 30);
		lblXNote.setLayoutData(fd_lblXNote);
		txtXNote = new WText(comXNote, SWT.BORDER, "", "备注");
		txtXNote.setTextLimit(128);
		FormData fd_txtXNote = new FormData();
		fd_txtXNote.left = new FormAttachment(lblXNote, 4);
		fd_txtXNote.top = new FormAttachment(comXNote, 2);
		fd_txtXNote.right = new FormAttachment(100, -8);
		fd_txtXNote.bottom = new FormAttachment(txtXNote, 30);
		txtXNote.setLayoutData(fd_txtXNote);
	
		//mainCom.pack();
		
		this.texts.add(txtXField);
		this.texts.add(txtXName);
		//this.texts.add(txtXNote);
		show();
		self.pack();
		ShellUtil.center(self);
		
		btnSave.setEnabled(false);
		btnClear.setEnabled(false);
	}

	
	
	@Override
	protected boolean checkModelChange() {
		boolean rs = false;
		if(model != null) {
			rs = !model.getStr("xField").equals(txtXField.getText())
					|| !model.getStr("xName").equals(txtXName.getText())
					|| !model.getStr("xNote").equals(txtXNote.getText());
		}else {
			rs = !txtXName.getText().equals("") || !txtXNote.getText().equals("") || !txtXField.getText().equals("");
		}
		return rs;
	}

	@Override
	protected boolean save() {
		// 校验
		if(this.txtXField.getText() == null || this.txtXField.getText().length() == 0 || this.txtXField.getText().length() > this.txtXField.getTextLimit()) {
			Msg.showMessage("错误", "字段错误");
			return false;
		}
		if(this.txtXName.getText() == null || this.txtXName.getText().length() == 0 || this.txtXName.getText().length() > this.txtXName.getTextLimit()) {
			Msg.showMessage("错误", "名称错误");
			return false;
		}
		if(this.txtXNote.getText() != null && this.txtXNote.getText().length() > this.txtXNote.getTextLimit()) {
			Msg.showMessage("错误", "备注超过限制");
			return false;
		}
		if(sID <= 0 || model == null) {
			//model = new YTDic().setSID(self.sID);
			model = new YTName().setSID(self.sID);
		}
		model.setXName(this.txtXName.getText()).setXNote(this.txtXNote.getText()).setXField(txtXField.getText());
		if(sID <= 0) {
			model.setROwnerID(dda.user.getSID()).setRAuthorID(dda.user.getSID())
			.setRMenderID(0).setRCheckerID(0).setRAuditorID(0).setSStatus(2);
		}
		if(this.sID > 0) {
			return masterService.update(model);
		}else {
			return masterService.save(model);
		}
	}

	@Override
	protected void show() {
		txtXField.removeModifyListener(modifyListener);
		txtXName.removeModifyListener(modifyListener);
		txtXNote.removeModifyListener(modifyListener);
		
		if(model != null) {
			txtXField.setText(model.getStr("xField"));
			txtXName.setText(model.getStr("xName"));
			txtXNote.setText(model.getStr("xNote"));
		
		}else {
			//txtSID.setText(String.valueOf(DDA.getInstance().getIDService.exec("yp_getid", 0, "yt_dic", 1)));
			txtXField.setText("");
			txtXName.setText("");
			txtXNote.setText("");
		
		}
		txtXField.addModifyListener(modifyListener);
		txtXName.addModifyListener(modifyListener);
		txtXNote.addModifyListener(modifyListener);
	}
	
	
	
	@Override
	protected void initBtnStatus(boolean isInit) {
		super.initBtnStatus(isInit);
//		boolean enable = false;
//		if(this.model != null && this.model.getSStatus().getSID() != 2 && self.btnEdit.getEnabled()) {
//			enable = false;
//		}else {
//			enable = true;
//		}
//		if(this.model == null || (this.model != null && this.model.getSStatus().getSID() != 2)) {
//			enable = false;
//		}
//		btnSave.setEnabled(!isInit);
//		this.btnClear.setEnabled(!isInit);
	}


	
}
